import json
import time

import requests

#token = "80f864dc42d3f2e200da93ca195d9db685def0e258d403203c0b24f3daee9799"     #我的token
token="007fc6e94a7aaa9c0f059ae0ffb5b1227fa0948c39c321f87030e712dfadbf6e"        #wkl的token
urlLogin = "https://openapiv51.ketangpai.com//CourseApi/semesterCourseList"
headersLogin = {
    "Request Method": "POST",
    "token": token,
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36",
    "isstudy": "1",
    "search": "",
    "semester": "2021-2022",
    "term": "2",
    "reqtimestamp": "'.time().'"
}


#  "mstatus": 0:未提交 1:已提交 2:已批改阅分 3:已批改

def Login(token):
    result = requests.post(urlLogin, headersLogin)
    userData = result.json()
    # print(userData['status'])
    if userData['status'] == 1:
        return userData['data']


url2 = "https://openapiv51.ketangpai.com//FutureV2/CourseMeans/getCourseContent"


# print(result.status_code)
# print(result.json())

def Getnofinish_commit_work(stt):
    stt=""
    finish_status = 0#判断有无未完成作业
    contexts = Login(token)
    # print(type(contexts))
    # print(contexts)
    # 打印还没超时但未提交作业
    print("---以下是未完成提交但未超时的作业！！！---")
    for context in contexts:
        id = context['id']
        headers2 = {
            "Request Method": "POST",
            "token": token,
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36",
            "contenttype": 4,
            "dirid": 0,
            "lessonlink": [],
            "sort": [],
            "page": 1,
            "limit": 100,
            "desc": 3,
            "courserole": 0,
            "reqtimestamp": 1639575903985,
            "courseid": id
        }

        contextInfo = requests.post(url2, headers2)
        contextInfoM = contextInfo.json()
        # print(contextInfoM['data']['list'])
        i = 0

        if contextInfoM['data']['list'] != []:
            for contextWork in contextInfoM['data']['list']:
                i = i + 1
                res = int(contextWork['endtime'])
                if time.time() < float(contextWork['endtime']) and contextWork['mstatus'] == 0:
                    endTime = time.strftime("%Y--%m--%d %H:%M:%S", time.localtime(res))
                    #print("课程名：" + "[-- " + context['coursename'] + " --]\n作业：[ " + contextWork['title'] + " ] End Time: " + endTime, end="")
                    stt="课程名：" + "[-- " + context['coursename'] + " --]\n作业：[ " + contextWork['title'] + " ] End Time: " + endTime+"\n"
                    if res < time.time():
                        print("!!!该作业超时未交!!!", end="")
                        stt+="!!!该作业超时未交!!!"

                    # print('End time: ' + endTime)
        else:
            finish_status=1
    if finish_status==1:
        stt="暂无未完成提交的作业！！！"
        #print("暂无未完成提交的作业！！！")
    return stt

def Getfinish_work(str):
    str=""
    contexts = Login(token)
    # print(type(contexts))
    # print(contexts)
    # 打印已经提交的作业
    print("---以下是已完成提交的作业！！！---")
    for context in contexts:
        id = context['id']
        headers2 = {
            "Request Method": "POST",
            "token": token,
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36",
            "contenttype": 4,
            "dirid": 0,
            "lessonlink": [],
            "sort": [],
            "page": 1,
            "limit": 100,
            "desc": 3,
            "courserole": 0,
            "reqtimestamp": 1639575903985,
            "courseid": id
        }

        contextInfo = requests.post(url2, headers2)
        contextInfoM = contextInfo.json()
        #print(contextInfoM['data']['list'])
        i = 0

        if contextInfoM['data']['list'] != []:
            #print(context['coursename'],end="----->")#打印课程名
            for contextWork in contextInfoM['data']['list']:
                i = i + 1
                res = int(contextWork['endtime'])
                # 打印已经交的作业
                if contextWork['mstatus'] == 1:
                    endTime = time.strftime("%Y--%m--%d %H:%M:%S", time.localtime(res))
                    #print("作业：[ " + contextWork['title'] + " ] End Time: " + endTime)
                    str+="课程名："+"[-- " + context['coursename'] + " --]\n作业：[ " + contextWork['title'] + " ] End Time: " + endTime+"\n"
                    #print(str)
    return str
                    #print()
                    #print(type(time.time()))  # 打印自从1970年1月1日午夜（历元）经过了多长时间，以秒为单位
                    #print(type(contextWork['endtime']))




if __name__ == '__main__':
    print(Getfinish_work(str))# 打印已经提交的作业
    print()
    stt=""
    print(Getnofinish_commit_work(stt))# 打印还没超时但未提交作业

